package com.lonni.rpc.core.spring;

import com.lonni.rpc.core.commom.BalanceType;
import com.lonni.rpc.core.loadbalance.BaseLoadbalance;
import com.lonni.rpc.core.loadbalance.RandomLoadBalance;
import com.lonni.rpc.core.loadbalance.wheel.RoundRobinLoadBalance;
import com.lonni.rpc.core.model.RpcConfig;
import com.lonni.rpc.core.register.ServiceDiscovery;
import com.lonni.rpc.core.register.ServiceRegister;
import com.lonni.rpc.core.register.zk.ZookeeperDiscovery;
import com.lonni.rpc.core.register.zk.ZookeeperRegister;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.io.Serializable;

/**
 * 配置类
 *
 * @author: Lonni
 * @date: 2024/7/10 0010 17:05
 */

@Configuration
@Slf4j

public class RpcSrpingAutoConfiguration implements Serializable {

    private final RpcConfig rpcConfig;


    public RpcSrpingAutoConfiguration(RpcConfig rpcConfig) {
        this.rpcConfig = rpcConfig;
    }

    /**
     * 初始化负载均衡
     *
     * @param rpcConfig
     * @return
     */
    @Bean
    public BaseLoadbalance loadbalance() {
        log.info("初始化默认负载均衡器{}", rpcConfig.getBalance());
        if (rpcConfig.getBalance().equals(BalanceType.RANDOM.getName())) {
            return new RandomLoadBalance();
        } else if (rpcConfig.getBalance().equals(BalanceType.QUEUE.getName())) {
            return new RoundRobinLoadBalance();
        }
        return null;
    }

    /**
     * 注册中心
     *
     * @return
     */
    @Bean
    public ServiceRegister register() {
        log.info("初始化默认注册中心");
        return new ZookeeperRegister();
    }

    /**
     * 服务发现
     *
     * @return
     */
    @Bean
    public ServiceDiscovery discovery() {
        log.info("初始化默认服务发现器");
        return new ZookeeperDiscovery();
    }
}



